﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace EMR
{
    public partial class AddTemplate : Form
    {
         private System.Xml.XmlDocument mXmlDoc;
        string filename = "../../TemplateStore";
        private Template _template = new Template();
        private string _Type;
        public AddTemplate()
        {
            _template.IsMultiple = 0;
            _template.IsMust = 0;
            InitializeComponent();
            this.rdb_AppliesCommon.Checked = true;
            this.rdb_biaozhun.Checked = true;
            
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            if(this.txb_Name.Text=="")
            {
                MessageBox.Show("模板名不能为空！");                                                //给出错误提示；
                this.txb_Name.Focus();                                                            //用户号文本框获得焦点；
                return;
            }
            foreach (var control in this.groupBox2.Controls)                       //遍历分组框中的控件集合；
            {
                RadioButton radioButton = control as RadioButton;                       //将控件转为单选按钮；
                if (radioButton.Checked)                                                //若单选按钮被选中；
                {
                    this._Type = radioButton.Text;                               //则获取单选按钮的文本；
                    break;
                }
            }
            _template.Name = this.txb_Name.Text;
         //   _template.IsMultiple = this.cbx_IsMust.Checked == true ? 1:0 ;
          //  _template.IsMust = this.cbx_IsMust.Checked == true ? 1 : 0;
            foreach (var control in this.groupBox1.Controls)                       //遍历分组框中的控件集合；
            {
                RadioButton radioButton = control as RadioButton;                       //将控件转为单选按钮；
                if (radioButton.Checked)                                                //若单选按钮被选中；
                {
                    this._template.AppliesGender = radioButton.Text;                               //则获取单选按钮的文本；
                    break;
                }
            }
            if(_template.AppliesGender== "通用模板")
            {
                _template.AppliesGender = "通用";
            }
            if (_template.AppliesGender == "女性模板")
            {
                _template.AppliesGender = "女";
            }
            if (_template.AppliesGender == "男性模板")
            {
                _template.AppliesGender = "男";
            }
            this._template.Remark = this.txb_Remark.Text;

      
            if(TemplateBll.AddTemplate(_template))
            {
                string linshi = @"../../image/新建模板.docx";
                string pa = filename + '/' + _Type + '/' + txb_Name.Text+".xml";
                //File.Create(linshi);
                
                //string strFileName;
                //strFileName = pa;
                // TemplateDesign templateDesign = new TemplateDesign(pa);

                System.IO.FileStream inFile = new FileStream(linshi, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] binaryData = new byte[inFile.Length];
                inFile.Read(binaryData, 0, (int)inFile.Length);
                string mStr = Convert.ToBase64String(binaryData);
                string hh = mStr;
                mXmlDoc = new System.Xml.XmlDocument();
                mStr = string.Format("<wawa><me aa=\"{0}\"/></wawa>", mStr); //wawa可改 aa要和后面一起改
                mXmlDoc.LoadXml(mStr);
              //  pa = pa.Remove(pa.Length - 3, 3);
                mXmlDoc.Save(pa); //保存
               // MessageBox.Show("保存成功");
                


                

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
          //  MessageBox.Show(TemplateBll.AddTemplate(_template).ToString());
         


        }

        private void btn_Abort_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void lbl_Name_Click(object sender, EventArgs e)
        {

        }

        private void cbx_IsMust_CheckedChanged(object sender, EventArgs e)
        {
            this._template.IsMust = (this._template.IsMust + 1) % 2;
        }

        private void cbx_IsMore_CheckedChanged(object sender, EventArgs e)
        {
            this._template.IsMultiple = (this._template.IsMultiple + 1)%2;
        }

        private void txb_Remark_TextChanged(object sender, EventArgs e)
        {

        }

        private void lbl_Remark_Click(object sender, EventArgs e)
        {

        }

        private void txb_Name_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
